package com.heitian.ssm.controller;

import cc.openkit.common.KitUtil;
import com.alibaba.fastjson.JSONObject;
import com.heitian.ssm.model.AddressList;
import com.heitian.ssm.model.RecentContacts;
import com.heitian.ssm.service.AddressListService;
import com.heitian.ssm.service.RecentContactsService;
import com.heitian.ssm.util.Pager;
import com.heitian.ssm.util.StaticFinalVar;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/recentContacts")
public class RecentContactsController {
    private Logger log = Logger.getLogger(RecentContactsController.class);
    @Resource
    private RecentContactsService recentContactsService;
    @Resource
    private AddressListService addressListService;

    /**
     * 添加
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Object add(HttpServletRequest request) {
        log.info("添加最近联系人");
        String id = request.getParameter("id");//id
        String place = request.getParameter("place");//分机号

        if(!KitUtil.feikong(place)||!KitUtil.feikong(id)){
            return JSONObject.toJSON(KitUtil.returnMap("4001", StaticFinalVar.PARAMETER_ERR));
        }
        RecentContacts recentContacts = recentContactsService.selectById(id, place);

        if(recentContacts!=null){
            recentContacts.setRecentContactsTime(new Date());
            return JSONObject.toJSON(recentContactsService.update(recentContacts) == 1 ? KitUtil.returnMap("200", "SUCCESS") : KitUtil.returnMap("101", "FAIL"));
        }else{
            recentContacts = new RecentContacts();
            recentContacts.setAddressListId(id);
            recentContacts.setRecentContactsPlace(place);
            recentContacts.setRecentContactsId(KitUtil.uuid());
            recentContacts.setRecentContactsTime(new Date());
            return JSONObject.toJSON(recentContactsService.save(recentContacts) == 1 ? KitUtil.returnMap("200", "SUCCESS") : KitUtil.returnMap("101", "FAIL"));
        }
    }
    @RequestMapping("/getJson")
    @ResponseBody
    public Object getJson(HttpServletRequest request){
        log.info("获取通讯录数据");
        String search = request.getParameter("search");
        String place = request.getParameter("place");

        List<AddressList> list = addressListService.selectRecentAddressList(search,place);

        Map<String,Object> map = new HashMap<String,Object>();
        // 返回数据
        map.put("code",0);
        map.put("msg","");
        map.put("data",list);

        return JSONObject.toJSON(map);
    }
    /**
     * 删除
     *
     * @param request
     * @return
     */
//    @RequestMapping(value = "/del", method = RequestMethod.POST)
//    @ResponseBody
//    public Object del(HttpServletRequest request) {
//        log.info("通讯录 》 删除");
//        String id = request.getParameter("id");
//        return addressListService.delete(id) > 0 ? KitUtil.returnMap("200", StaticFinalVar.DEL_OK) : KitUtil.returnMap("101", StaticFinalVar.DEL_ERR);
//    }

}
